<?php
/**
 * 智慧停车系统模块微站定义
 *
 * @author 橙橙同学
 * @url 
 */

//加载BM微框架
require_once 'bm/bootstrap.php';
use \app\Model\ParkOrderModel;
use \app\Model\ParkMemberModel;
defined('IN_IA') or exit('Access Denied');

class Orange_parkModuleSite extends WeModuleSite {


    public function doMobileDevCommonTipsSuccess()
    {
        include $this->template('commontips/success');
    }

    public function doMobileDevCommonTipsError()
    {
        include $this->template('commontips/error');
    }

    public function doMobileDevParkOrder()
    {
        include $this->template('park/parkorder');
    }

    public function doMobileDevBindPlatenumber()
    {
        include $this->template('park/bindplatenumber');
    }

    public function doMobileDevBindParkRecord()
    {
        include $this->template('park/bindplatenumber');
    }



    /**
     * @param $message
     * @author:橙橙同学
     * @func:公共提示
     */
    protected function commonTips($message,$redirect = '',$type)
    {
        if($redirect == '')
        {
            $redirectUrl = $_SERVER['HTTP_REFERER'];
        }else{
            $redirectUrl = $redirect;
        }
        switch ($type){
            case "success":
                message($message,$redirectUrl,'success');
//                $this->template('commontips/success');
                break;
            case "info":
                message($message,$redirectUrl,'info');
//                $this->template('commontips/success');
                break;
            case "error":
                message($message,$redirectUrl,'error');
//                $this->template('commontips/error');
                break;
            default:
                message($message,$redirectUrl,'success');
//                $this->template('commontips/success');
        }
    }

    protected function commonApiCheckOpenid($openid)
    {
        if(empty($openid))
        {
            $this->commonApiReturn(-1,'','请在微信端打开本页面');
            exit();
        }
    }

    protected function commonApicheckUid($uid)
    {
        if(empty($uid))
        {
            $this->commonApiReturn(-1,'','您还不是会员');
            exit();
        }
    }


    /**
     * @author:橙橙同学
     * @func:公共检测微信
     */
    protected function commonCheckOpenid()
    {
        global $_W;
        $openid = $_W['openid'];
        if(empty($openid))
        {
            $this->commonTips('请确保在微信端打开本页面','','info');
            exit();
        }
    }


    /**
     * @param $openid
     * @author:橙橙同学
     * @func:公共检测会员
     */
    protected function commonCheckUid($uid)
    {
        if(empty($uid))
        {
            $this->commonTips('您还不是会员','','info');
            exit();
        }
    }

    /**
     * @author:橙橙同学
     * @func:格式化API返回信息
     */
    protected function commonApiReturn($errCode = 0,$data = '',$msg = '')
    {
        $return = array(
            'errCode'=>$errCode,
            'data'=>$data,
            'msg'=>$msg
        );
        echo json_encode($return);
        exit();
    }


    /**
     * @param $uid
     * @return bool
     * @author:橙橙同学
     * @func:获取会员信息
     */
    protected function getMemberInfoByUid($uid)
    {
        global $_W;
        $memberInfo = pdo_fetch("select * from ".tablename('mc_members')." where `uniacid`=:uniacid
        and `uid`=:uid",array(':uniacid'=>$_W['uniacid'],':uid'=>$uid));
        return $memberInfo;
    }



    protected function checkParkOrder($uid=null,$park_order_id=null)
    {
        //false 统一为订单信息不合法
        if(!empty($park_order_id))
        {
            //根据park_order_id and uid 联合查询订单信息
            $parkRes = ParkOrderModel::getParkOrderByIdAndUid($uid,$park_order_id);
            if($parkRes){
                return $parkRes;
            }
            else{
                return false;
            }
        }else{
            return false;
        }
    }


    /**
     * @author:橙橙同学
     * @func:停车订单页
     */
    public function doMobileParkOrder()
    {
        global $_W,$_GPC;
        $openid = $_W['openid'];
        $uid = mc_openid2uid($openid);
        //检测openid 截断
        $this->commonCheckOpenid($openid);
        //检测uid 截断
        $this->commonCheckUid($uid);
        $park_order_id = $_GPC['park_order_id'];

        $parkRes = $this->checkParkOrder($uid,$park_order_id);
        if($parkRes)
        {
            //渲染订单详情页面
            include $this->template('park/parkorder');
        }else {
            //Tips:订单不合法
        }
    }

    /**
     * @author:橙橙同学
     * @func:停车记录页
     */
    public function doMobileParkRecord()
    {
        include $this->template('park/parkrecord');
    }

    /**
     * @author:橙橙同学
     * @func:绑定车牌页面
     */
    public function doMobileBindPlateNumber()
    {
        global $_W,$_GPC;
        $openid = $_W['openid'];
        $uid = mc_openid2uid($openid);
        //检测openid 截断
        $this->commonCheckEmptyOpenid($openid);

        include $this->template('park/bindplatenumber');
    }

    /**
     * @author:橙橙同学
     * @func:API 绑定车牌
     */
    public function doMobileApiBindPlateNumber()
    {
        global $_W,$_GPC;
        $openid = $_W['openid'];
        $platenumber = $_W['platenumber'];

        if(empty($platenumber))
        {
            $this->commonApiReturn(-1,'','未传入车牌');
        }

        if(empty($_GPC['openid']) || $openid!=$_GPC['openid'])
        {
            $this->commonApiReturn(-1,'','openid不合法');
        }else{

            $uid = mc_openid2uid($openid);
            if(!empty($uid))
            {
                //绑定用户车牌
                //TODO 去MODEL层编写绑定车牌的数据库操作
                $bindPlatenumberRes = ParkMemberModel::bindMemberPlateNumber($uid);
                if($bindPlatenumberRes)
                {
                    //在这里我想可以发个模板消息什么的，推个会员权益啥的
                    $this->commonApiReturn(1,'','绑定车牌成功');
                }
                else
                {
                    $this->commonApiReturn(-1,'','绑定车牌失败，请重试');
                }
            }
            else{
                $this->commonApiReturn(-1,'','用户还未成为微会员，请领取会员卡成为会员');
            }
        }
    }


	public function doMobileEntry() {
		//这个操作被定义用来呈现 功能封面

        global $_W;
        // 一些业务代码。
        //构造支付请求中的参数
        $params = array(
            'tid' => '1111',      //充值模块中的订单号，此号码用于业务模块中区分订单，交易的识别码
            'ordersn' => '2222',  //收银台中显示的订单号
            'title' => '系统充值余额',          //收银台中显示的标题
            'fee' => 10,      //收银台中显示需要支付的金额,只能大于 0
            'user' => $_W['member']['uid'],     //付款用户, 付款的用户名(选填项)
        );

        $this->pay($params);
	}
	public function doWebParkrecordmanage() {

		//这个操作被定义用来呈现 管理中心导航菜单
	}
	public function doWebParkmembermanage() {
		//这个操作被定义用来呈现 管理中心导航菜单
	}
	public function doMobileIndexnav() {
		//这个操作被定义用来呈现 微站首页导航图标
	}
	public function doMobilePersonalnav() {
		//这个操作被定义用来呈现 微站个人中心导航
	}
	public function doMobileQknav() {
		//这个操作被定义用来呈现 微站快捷功能导航
	}

}